<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pager {
	private $model_obj = null;
	private $pageData = null;
	private $page = null;
	private $pagesize = null;
	
	public function init(&$obj,$page,$pagesize){
		$this->model_obj = $obj;
		$this->page = $page;
		$this->pagesize = $pagesize;
		return $this;
	}
	
	public function __call($func_name, $func_args){
		if( ( 'findAllJoin' == $func_name || 'findAll' == $func_name || 'findSql' == $func_name ) && isset($this->page) && isset($this->pagesize)){
			return $this->runpager($func_name, $func_args);
		}elseif(method_exists($this,$func_name)){
			return call_user_func_array(array($this, $func_name), $func_args);
		}else{
			return call_user_func_array(array($this->model_obj, $func_name), $func_args);
		}
	}
	
	public function getPager(){
		return $this->pageData;
	}

	private function runpager($func_name, $func_args){
		$this->pageData = null;
		$page = $this->page;
		$pageSize = $this->pagesize;
		@list($conditions, $sort, $fields ) = $func_args;
		$total_count = ('findAllJoin'==$func_name)?$this->model_obj->findCountJoin($conditions):$this->model_obj->findCount($conditions);
		$total_page = ceil( $total_count / $pageSize );
		$page_for_pageData = min(intval(max($page, 1)), $total_count);
		$this->pageData = array(
			"total_count" => $total_count,                                 
			"page_size"   => $pageSize,                                   
			"total_page"  => $total_page,                                  
			"first_page"  => 1,                                           
			"prev_page"   => ( ( 1 == $page_for_pageData ) ? 1 : ($page_for_pageData - 1) ),
			"next_page"   => ( ( $page_for_pageData == $total_page ) ? $total_page : ($page_for_pageData + 1)),     
			"last_page"   => $total_page,                                
			"current_page"=> $page_for_pageData,                        
			"all_pages"   => array()	                                 
		);
		for($i=1; $i <= $total_page; $i++)$this->pageData['all_pages'][] = $i;
		$limit = ($page - 1) * $pageSize . "," . $pageSize;
		return ('findAllJoin'==$func_name)?$this->model_obj->findAllJoin($conditions, $sort, $fields, $limit):$this->model_obj->findAll($conditions, $sort, $fields, $limit);
	}

	function createCustomPages($ma,$args,$pkey='page',$pagesize=null,$numpages=10){
		if($this->pageData['total_page']==1)
			return '';
		$pager = $this->pageData;
		if(!$pagesize){
			$pagesize = $pager['page_size'];
		}else{
			$current_page = ceil(($pager['page_size']/$pagesize)*$pager['current_page']);
			$pager['current_page'] = $current_page;
			$pager['page_size'] = $pagesize;
			$pager['total_page'] = ceil( $pager['total_count'] / $pagesize );
			$pager['prev_page'] = ( ( 1 == $current_page ) ? 1 : ($current_page - 1) );
			$pager['next_page'] = ( ( $current_page == $pager['total_page'] ) ? $pager['total_page'] : ($current_page + 1));
			$pager['last_page'] = $pager['total_page'];
			$pager['all_pages'] = array();
			for($i=1; $i <= $pager['total_page']; $i++)$pager['all_pages'][] = $i;
			$this->pageData = $pager;
		}

		return $this->createPages($ma, $args,$pkey,$numpages);
	}
	
	
	function createPages($ma,$args,$pkey='page',$numpages=10){
		if($this->pageData['total_page']==1)
			return '';
		$pager = $this->pageData;
		$midpage = $numpages/2;
		$pages = $pager['all_pages'];
		$currentpage = $pager['current_page'];
		$totalpage = $pager['total_page'];
		$lastpage = $pager['last_page'];
		$nextpage = $pager['next_page'];
		$prevpage = $pager['prev_page'];
		$firstpage = $pager['first_page'];
	
		$des = '<div class="pagination"><ul>';
		if ($currentpage != $firstpage&&$prevpage>0){
			$args[$pkey] = $prevpage;
			$des .= '<li><a href="'.urlwithparam($ma, $args).'">'.lang('prev_page','common').'</a></li>';
		}
	
		if($totalpage>$numpages){
			if($currentpage<$midpage){
				for ($i=0;$i<$numpages;$i++){
					if ($pages[$i] != $currentpage) {
						$args[$pkey] = $pages[$i];
						$des .= '<li><a href="'.urlwithparam($ma, $args).'">'.$pages[$i].'</a></li>';
					}else{
						$des .= '<li class="active"><a href="javascript:;">'.$pages[$i].'</a></li>';
					}
				}
	
			}else{
				$start = $currentpage-$midpage;
				$end = $currentpage+$midpage;
				$end = ($pages[$end])?$end:($lastpage);
				if($start>0){
					$args[$pkey] = $firstpage;
					$des .= '<li><a href="'.urlwithparam($ma, $args).'">1...</a></li>';
				}
				for ($i=$start;$i<$end;$i++){
					if ($pages[$i] != $currentpage) {
						$args[$pkey] = $pages[$i];
						$des .= '<li><a href="'.urlwithparam($ma, $args).'">'.$pages[$i].'</a></li>';
					}else{
						$des .= '<li class="active"><a href="javascript:;">'.$pages[$i].'</a></li>';
					}
				}
			}
			$off = $currentpage+$midpage-1;
			if($pages[$off]&&($pages[$off]<$lastpage)){
				$args[$pkey] = $lastpage;
				$des .= '<li><a href="'.urlwithparam($ma, $args).'">...'.$lastpage.'</a></li>';
			}
	
		}else{
			for ($i=0;$i<$totalpage;$i++){
				if ($pages[$i] != $currentpage) {
					$args[$pkey] = $pages[$i];
					$des .= '<li><a href="'.urlwithparam($ma, $args).'">'.$pages[$i].'</a></li>';;
				}else{
					$des .= '<li class="active"><a href="javascript:;">'.$pages[$i].'</a></li>';
				}
			}
		}
	
		if ($pager['current_page'] != $lastpage){
			$args[$pkey] = $nextpage;
			$des .= '<li><a href="'.urlwithparam($ma, $args).'">'.lang('next_page','common').'</a></li>';
		}
		$des .= '</ul></div>';
		return $des;
	}
}
?>